//
//  TopicListView.m
//  YoursShow
//
//  Created by Roc on 11-4-12.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TopicListView.h"
#import "PhotoTopic.h"
#import "Topic.h"
#import "PhotoTopicView.h"

@implementation TopicListView

@synthesize topicListViewDelegate = _topicListViewDelegate;

@synthesize topicSource = _topicSource;

- (UIView*)createTopicView:(CGRect)rect topic:(id<Topic>)topic {
    if ([topic conformsToProtocol:@protocol(PhotoTopic)]) {
        PhotoTopicView* topicView = [[PhotoTopicView alloc] initWithFrame:rect
                                                    topic:(id<PhotoTopic>)topic];
        topicView.topicViewdelegate = self;
        return topicView;
    }
    
    return nil;
}

- (void)addTopicView:(CGRect)rect topic:(id<Topic>)topic{
    UIView* topicview = [self createTopicView:rect topic:topic];

    [self addSubview:topicview];
    [topicview release];
}

- (id)initWithFrame:(CGRect)frame topicSource:(id<TopicSource>)topicSource;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.topicSource = topicSource;
        
        self.contentSize = CGSizeMake(500, _topicSource.numberOfTopics/2*256);
        self.backgroundColor = [UIColor clearColor];
        
        CGRect rect;
        NSInteger index = 1;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 2; j++) {
                rect = CGRectMake(256*j, 12+i*256, 232, 232);
                [self addTopicView:rect topic:[topicSource topicAtIndex:index-1]];
                index++;
            }
        }
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

#pragma mark TopicViewDelegate

-(void)onTap:(id<TopicView>)topicView {
    if (_topicListViewDelegate) {
        [_topicListViewDelegate topicSelected:topicView.topic];
    }
}

@end
